package de.mmis.core.base.abstracttree.serializer.selector;

import java.util.Map;

import de.mmis.core.base.gclass.GClass;

/**
 * {@link InstanceSelector} returning an object in the reference map
 * 
 * @author Christian Koch
 */
public class IDSelector extends InstanceSelector {

	public IDSelector(String id, Map<String, Object> idmap) {
		super(id, idmap);
	}

	@Override
	public Object createInstance(GClass gclass)
			throws InstanceSelectorException {
		if (getIDMap() != null && getIDMap().containsKey(getID())) {
			Object obj = getIDMap().get(getID());
			if (obj != null
					&& !SimpleSelector.checkType(obj.getClass(), gclass))
				throw new InstanceSelectorException("object \"" + obj
						+ "\" of class " + obj.getClass()
						+ " cannot be converted to type " + gclass);
			return obj;
		}

		throw new InstanceSelectorException("label " + getID()
				+ " points to nonexisting object");
	}

}
